﻿Public Class frmContractEntry

    Dim _ID As Long
    Dim isInserted As Boolean = False
    Dim _EndDate As Date
    Public _ContractID As Long

    Private Sub frmContractEntry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getComboLawyer()
        getHeaderGrid_File()
        getHeaderGrid_Description()
        reset()
        If _ContractID <> 0 Then
            checkFinish()
            getTextbox()
        Else
            isInserted = False
            inserted_Contract_ID()
        End If
    End Sub

    Protected Sub checkFinish()
        Dim dt As New DataTable
        Dim oContract As New cContract
        dt = oContract.select_Contract_byID(_ContractID)
        If dt.Rows(0)("IsFinish") Then
            EnableControl_All()
        Else
            EnableControl()
        End If
    End Sub

    Protected Sub getHeaderGrid_File()
        Dim HeaderText As String() = {"លេខរៀង", "ID", "ឯកសារ", "ថ្ងៃបញ្ចូល", "អ្នកបញ្ចូល"}
        Dim HeaderWidth As Integer() = {100, 100, 250, 170, 200}
        Dim i As Integer
        dgvFile.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            dgvFile.Columns(i).HeaderText = HeaderText(i)
            dgvFile.Columns(i).Width = HeaderWidth(i)
        Next
        dgvFile.Columns(1).Visible = False
        dgvFile.Columns(3).DefaultCellStyle.Format = "dd/MM/yyyy hh:mm tt"
    End Sub

    Protected Sub getHeaderGrid_Description()
        Dim HeaderText As String() = {"លេខរៀង", "ID", "សេចក្តីអធិប្បាយ", "ថ្ងៃបញ្ចូល", "អ្នកបញ្ចូល"}
        Dim HeaderWidth As Integer() = {100, 100, 250, 170, 200}
        Dim i As Integer
        dgvDescription.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            dgvDescription.Columns(i).HeaderText = HeaderText(i)
            dgvDescription.Columns(i).Width = HeaderWidth(i)
        Next
        dgvDescription.Columns(1).Visible = False
        dgvDescription.Columns(3).DefaultCellStyle.Format = "dd/MM/yyyy hh:mm tt"
    End Sub

    Public Sub getFile()
        If _ContractID <> 0 Then
            getFile_byContractID(_ContractID)
        Else
            dgvFile.Rows.Clear()
            Dim oFile As New cFile
            Dim dt As New DataTable
            Dim dr As DataRow
            Dim i As Integer = 1

            dt = oFile.select_File_Contract(_ID)
            For Each dr In dt.Rows
                dgvFile.Rows.Add(i, dr(0), dr(1), dr(2), dr(3))
                i += 1
            Next
        End If
    End Sub

    Public Sub getDescription()
        If _ContractID <> 0 Then
            getDescription_byContractID(_ContractID)
        Else
            dgvDescription.Rows.Clear()
            Dim oDescription As New cDescription
            Dim dt As New DataTable
            Dim i As Integer = 1

            dt = oDescription.select_Description_Contract(_ID)
            Dim dr As DataRow
            Dim richtextbox As New RichTextBox
            Dim Description As String
            For Each dr In dt.Rows
                richtextbox.Rtf = dr(1)
                Description = richtextbox.Text
                dgvDescription.Rows.Add(i, dr(0), Description, dr(2), dr(3))
                i += 1
            Next
        End If
    End Sub

    Protected Sub getTextbox()
        Dim oContract As New cContract
        Dim dt As New DataTable
        dt = oContract.select_Contract_byID(_ContractID)
        Try
            getComboCompany(_ContractID)
            cboLawyer.SelectedValue = dt.Rows(0)("LawyerID")
            txtFee.Text = dt.Rows(0)("ContractFee")
            txtDuration.Text = dt.Rows(0)("Duration")
            dtpStartContract.Value = dt.Rows(0)("StartDate")
            txtEndContract.Text = Format(dt.Rows(0)("EndDate"), "dd/MM/yyyy")
            getFile_byContractID(_ContractID)
            getDescription_byContractID(_ContractID)
        Catch ex As Exception

        End Try
    End Sub

    Public Sub getComboCompany(ByVal ContractID As Long)
        Dim oc As New cControl
        oc.getCombo(cboCompany, "SELECT CompanyID,CompanyName FROM tblCompany WHERE ContractID=N'" & ContractID & "'", "CompanyName", "CompanyID")
    End Sub

    Protected Sub getFile_byContractID(ByVal ContractID As Long)
        dgvFile.Rows.Clear()
        Dim oFile As New cFile
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oFile.select_File_Contract(ContractID)
        For Each dr In dt.Rows
            dgvFile.Rows.Add(i, dr(0), dr(1), dr(2), dr(3))
            i += 1
        Next
    End Sub

    Protected Sub getDescription_byContractID(ByVal ContractID As Long)
        dgvDescription.Rows.Clear()
        Dim oDescription As New cDescription
        Dim dt As New DataTable
        Dim i As Integer = 1

        dt = oDescription.select_Description_Contract_byContractID(ContractID)
        Dim dr As DataRow
        Dim richtextbox As New RichTextBox
        Dim Description As String
        For Each dr In dt.Rows
            richtextbox.Rtf = dr(1)
            Description = richtextbox.Text
            dgvDescription.Rows.Add(i, dr(0), Description, dr(2), dr(3))
            i += 1
        Next
    End Sub

    Public Sub EnableControl()
        cboCompany.Enabled = False
        cboLawyer.Enabled = False
        txtFee.Enabled = False
        txtDuration.Enabled = False
        dtpStartContract.Enabled = False
        txtEndContract.Enabled = False
        btnAddCompany.Text = "បង្ហាញពត៌មានក្រុមហ៊ុន"
        btnSave.Enabled = False
        btnShow.Enabled = False
    End Sub

    Public Sub EnableControl_All()
        cboCompany.Enabled = False
        cboLawyer.Enabled = False
        txtFee.Enabled = False
        txtDuration.Enabled = False
        dtpStartContract.Enabled = False
        txtEndContract.Enabled = False
        btnAddCompany.Text = "បង្ហាញពត៌មានក្រុមហ៊ុន"
        lblAddFile.Enabled = False
        lblAddDescription.Enabled = False
        btnSave.Enabled = False
        btnShow.Enabled = False
        btnFinishContract.Enabled = False
    End Sub

    Protected Sub inserted_Contract_ID()
        Dim oContract As New cContract
        Try
            _ID = getID()
            oContract.insert_Contract_ID(_ID)
        Catch ex As Exception
            MessageBox.Show("កម្មវិធីនេះមិនអាចបញ្ចូលកុងត្រាជាមួយក្រុមហ៊ុនបានទេ", "សូមអភ័យទោស", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Protected Function getID() As Integer
        Dim ID As Integer
        Dim oControl As New cControl
        ID = oControl.getID("ContractID", "tblContract")
        Return ID
    End Function

    Protected Sub getComboLawyer()
        Dim oControl As New cControl
        oControl.getCombo(cboLawyer, "SELECT LawyerID,LawyerName FROM tblLawyer WHERE Active=1", "LawyerName", "LawyerID")
    End Sub

    Protected Sub reset()
        cboCompany.SelectedIndex = -1
        If cboLawyer.Items.Count > 0 Then
            cboLawyer.SelectedValue = frmMain._LawyerID
        Else
            cboLawyer.SelectedIndex = -1
        End If
        txtFee.Clear()
        txtDuration.Clear()
        dtpStartContract.Value = Now
        txtEndContract.Clear()
        Dim i As Integer
        For i = 0 To dgvFile.Rows.Count - 1
            dgvFile.Rows.RemoveAt(0)
        Next
        For i = 0 To dgvDescription.Rows.Count - 1
            dgvDescription.Rows.RemoveAt(0)
        Next
        btnAddCompany.Enabled = True
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub btnAddCompany_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddCompany.Click
        If btnAddCompany.Text = "បញ្ចូលពត៌មានក្រុមហ៊ុន" Then
            frmCompany._ContractID = _ID
            frmCompany._CompanyID = 0
            frmCompany.OpenControl()
            frmCompany.ShowDialog()
        Else
            frmCompany._ContractID = _ContractID
            frmCompany._CompanyID = cboCompany.SelectedValue
            frmCompany.EnableControl()
            frmCompany.ShowDialog()
        End If
    End Sub

    Private Sub lblAddFile_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lblAddFile.LinkClicked
        If _ContractID <> 0 Then
            frmAttachFile._ContractID = _ContractID
        Else
            frmAttachFile._ContractID = _ID
        End If
        frmAttachFile.isWhere(True, False, False, False)
        frmAttachFile.ShowDialog()
    End Sub

    Private Sub lblAddDescription_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lblAddDescription.LinkClicked
        If _ContractID <> 0 Then
            frmDescription._ContractID = _ContractID
        Else
            frmDescription._ContractID = _ID
        End If
        frmDescription.isWhere(True, False, False, False)
        frmDescription.ShowDialog()
    End Sub

    Private Sub frmContractEntry_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        If isInserted = False Then
            deleted_Contract()
            deleted_Company()
            deleted_FileContract()
            deleted_Description_Contract()
        End If
    End Sub

    Protected Sub deleted_Contract()
        Dim oContract As New cContract
        oContract.delete_Contract_ID(_ID)
    End Sub

    Protected Sub deleted_Company()
        Dim oCompany As New cCompany
        oCompany.delelte_Company(_ID)
    End Sub

    Protected Sub deleted_FileContract()
        Dim oFile As New cFile
        oFile.delete_File_Contract(_ID)
    End Sub

    Protected Sub deleted_Description_Contract()
        Dim oDescription As New cDescription
        oDescription.delete_Description_Contract(_ID)
    End Sub

    Protected Function checknull() As Boolean
        Dim oc As New cControl
        If oc.checknull(cboCompany.Text) And oc.checknull(cboLawyer.Text) And oc.checknull(txtFee.Text) And oc.checknull(txtDuration.Text) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        inserted()
    End Sub

    Protected Sub inserted()
        Dim oContract As New cContract
        If checknull() Then
            If oContract.insert_Contract(_ID, cboLawyer.SelectedValue, CDbl(txtFee.Text), CInt(txtDuration.Text), dtpStartContract.Value, _EndDate, frmMain._User) Then
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                isInserted = True
                'reset()
                frmContractEntry_Load(Nothing, Nothing)
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub dtpStartContract_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpStartContract.ValueChanged
        calEndDate()
    End Sub

    Private Sub txtDuration_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDuration.TextChanged
        calEndDate()
    End Sub

    Protected Sub calEndDate()
        If txtDuration.Text <> "" Then
            _EndDate = dtpStartContract.Value.AddMonths(CInt(txtDuration.Text))
            txtEndContract.Text = Format(_EndDate, "dd/MM/yyyy")
        Else
            _EndDate = dtpStartContract.Value
            txtEndContract.Text = Format(_EndDate, "dd/MM/yyyy")
        End If
    End Sub

    Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
        If Application.OpenForms().OfType(Of frmContract).Any Then
            frmContract.rdContractIsNotFinish.Checked = True
            frmContract.getContract_NotFinish()
            Close()
        Else
            ShowForm(frmContract)
        End If
    End Sub

    Private Sub ShowForm(ByVal frm As Form)
        frm.MdiParent = Me.MdiParent
        frm.ControlBox = False
        frm.ShowIcon = False
        frm.WindowState = FormWindowState.Maximized
        Me.Close()
        frm.Show()
        frm.BringToFront()
    End Sub

    Private Sub btnFinishContract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinishContract.Click
        update_Contract_Finish_ClickButton()
    End Sub

    Protected Sub update_Contract_Finish_ClickButton()
        Dim oContract As New cContract
        Dim calDuration As Integer
        Dim calEndDate As Date = Now
        calDuration = DateDiff(DateInterval.Month, dtpStartContract.Value, Now)
        If MessageBox.Show("តើអ្នកប្រាកជាចង់បញ្ចប់សំណុំរឿងនេះមែនទេ?", "បញ្ចប់សំណុំរឿង", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            If oContract.update_Contract_Finish_ForClickButton(_ContractID, calDuration, calEndDate) Then
                EnableControl_All()
                frmContract.rdContractIsNotFinish.Checked = True
                frmContract.getContract_NotFinish()
                getTextbox()
                MessageBox.Show("បញ្ចប់រឿងក្តីបានសម្រេច!", "បញ្ចប់សំណុំរឿង", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("បញ្ចប់រឿងក្តីមិនបានសម្រេចទេ!", "បញ្ចប់សំណុំរឿង", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If
    End Sub

    Private Sub dgvFile_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvFile.DoubleClick
        If dgvFile.Rows.Count > 0 Then
            If _ContractID <> 0 Then
                frmViewImage._ContractID = _ContractID
            Else
                frmViewImage._ContractID = _ID
            End If
            frmViewImage._FileContractID = dgvFile.CurrentRow.Cells(1).Value
            frmViewImage.ShowDialog()
        End If
    End Sub

    Private Sub dgvDescription_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDescription.DoubleClick
        If dgvDescription.Rows.Count > 0 Then
            If _ContractID <> 0 Then
                frmViewDescription._ContractID = _ContractID
            Else
                frmViewDescription._ContractID = _ID
            End If
            frmViewDescription._DescriptionContractID = dgvDescription.CurrentRow.Cells(1).Value
            frmViewDescription.ShowDialog()
        End If
    End Sub
End Class